package com.example.vehicle.Performance;

import com.example.vehicle.Utils.HttpClientUtil;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;

/**
 * @name: Query
 * @description: 压力测试-查询。
 * @author: Lulu Zhao
 **/
public class Query implements Runnable {
    static double totalTime;

    /**
     * @name: run
     * @description: runnable接口run函数。
     * @params: []
     * @return: void
     * @author: Lulu Zhao
     */
    @Override
    public void run() {
        for (int i = 0; i < 5000; i++) {
            double startTime = System.nanoTime();
            HttpClientUtil.doGet("http://localhost:8080/chacc/chargingstations");
            double endTime = System.nanoTime();
            totalTime = endTime - startTime;
            try {
                String registerFileName = "src/query.txt";
                File registerFile = new File(registerFileName);
                FileWriter fileWriter = new FileWriter(registerFile, true);
                BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
                bufferedWriter.write(totalTime / 1000000 + "\n");
                bufferedWriter.close();
                fileWriter.close();
                System.out.println("查询操作" + totalTime / 1000000 + "ms");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
